unit Tablefm;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  System.UIConsts,FMX.Objects,FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs,
  FMX.StdCtrls, FMX.TabControl,UModel,UMyShape;

type
  TTableForm = class(TForm)
    TabControl1: TTabControl;
    procedure FormActivate(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    NeedRefresh : boolean;
    TableLayouts : TTablePageList;
    TextFont : TFont;
    procedure CreateTextFont;
    procedure SetPositionAndSize(shape:TShape;TheTable:TTable);
    function  GetTableColor(table : TTable):TAlphaColor;
    procedure RefreshTableStatus;
    procedure AddTablesToControl(index: Integer);
    Procedure SetShapeOfATable(table: TTable);
    procedure MyShapeClick(Sender: TObject);
  public
    { Public declarations }
    SelectTableNo : string;
    SelectPageNo : integer;
  end;

var
  TableForm: TTableForm;

implementation

{$R *.fmx}
 function TTableForm.GetTableColor(table : TTable):TAlphaColor;
 begin
   if table.Shape = 3 then
      result := claYellow
   else
     if table.OrderNo = '' then
        result := claGreen
     else
        result := claRed;
 end;


procedure TTableForm.FormActivate(Sender: TObject);
begin
   if NeedRefresh then
     RefreshTableStatus
   else
     NeedRefresh := true;
end;

procedure TTableForm.RefreshTableStatus;
begin
  // to do
end;

procedure TTableForm.FormCreate(Sender: TObject);
var
   i : integer;
   tabitem : TTabItem;
begin
  NeedRefresh := false;
  CreateTextFont;
  TableLayouts := TTablePageList.Create;
  for I := 0 to TableLayouts.TablePageList.Count - 1 do
  begin
     tabitem := TTabItem.Create(nil);
     with TableLayouts.TablePageList[i] do
     begin
        tabitem.Text := Description;
        tabitem.Tag := PageNo;
        tabitem.Parent := TabControl1;
        TabControl1.ActiveTab := tabitem;
        AddTablesToControl(i);
     end;
  end;
  TabControl1.ActiveTab:=TabControl1.Tabs[0];
end;

procedure TTableForm.CreateTextFont;
Begin
   TextFont := TFont.Create;
   TextFont.Size := 18;
   TextFont.Style := [TFontStyle.fsBold];
   TextFont.Family := 'Arial';
end;


procedure TTableForm.AddTablesToControl(index: Integer);
var i : integer;
    TheTable : TTable;
begin
   with TableLayouts do
   begin
     for I := 0 to TablePageList[index].Tables.Count - 1 do
     begin
       TheTable := TablePageList[index].Tables[i];
       SetShapeOfATable(TheTable);
     end;
   end;
end;

procedure TTableForm.MyShapeClick(Sender: TObject);
begin
   if Sender is TMyEllipse then
   begin
     SelectTableNo := (Sender as TMyEllipse).TagString;
     SelectPageNo := (Sender as TMyEllipse).Tag;
   end
   else
   begin
      SelectTableNo := (Sender as TMyRectAngle).TagString;
      SelectPageNo := (Sender as TMyRectAngle).Tag;
   end;
   self.ModalResult := mrOK;    // Notify the Orderfm a table has been selected
   self.CloseModal;
end;


procedure TTableForm.SetPositionAndSize(shape:TShape;TheTable:TTable);
begin
   shape.Position.X := TheTable.X;
   shape.Position.Y := TheTable.Y;
   shape.Width := TheTable.Width;
   shape.Height := TheTable.Height;

   if TheTable.IsTable then             // Add click handler to table shape
      shape.OnClick := self.MyShapeClick;
   shape.Tag := TheTable.PageNo;        // Stored the table's PageNo to shape
   shape.TagString := TheTable.TableNo; // Stored the table's TableNo to shape
End;

procedure TTableForm.SetShapeOfATable(table: TTable);
var
  ellipse : TMyEllipse;
  rectangle : TMyRectAngle;
begin
   case table.Shape of
     0:
      begin
         ellipse := TMyEllipse.Create(nil);
         SetPositionAndSize(ellipse,table);
         ellipse.Fill.Color := self.GetTableColor(table);
         ellipse.Text := table.Description;
         ellipse.Parent := TabControl1;
         ellipse.Font := TextFont;
         ellipse.TextColor := claBlack;
      end;
      else
      begin
        rectangle := TMyRectangle.Create(nil);
        SetPositionAndSize(rectangle,table);
        rectangle.Fill.Color := self.GetTableColor(table);
        rectangle.Text := table.Description;
        rectangle.Parent := TabControl1;
        rectangle.Font := TextFont;
        rectangle.TextColor := claBlack;
      end;
   end;
end;


end.
